package com.lqwu.mallchatserver.common.event.listener;

import com.lqwu.mallchatserver.chat.model.entry.Message;
import com.lqwu.mallchatserver.chat.service.MessageService;
import com.lqwu.mallchatserver.common.event.MessageSendEvent;

import com.lqwu.mallchatserver.websocket.service.WebSocketService;
import com.lqwu.mallchatserver.websocket.service.adapter.WSAdapter;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

import javax.annotation.Resource;

/**
 * 消息发送的监听器
 */
@Component
public class MessageSendListener {
    @Resource
    private MessageService messageService;
    @Resource
    private WebSocketService webSocketService;

    @Async
    @TransactionalEventListener(classes = MessageSendEvent.class,phase = TransactionPhase.AFTER_COMMIT)
    public void sendMsg(MessageSendEvent event) {
        Long msgId = event.getMsgId();
        long timestamp = event.getTimestamp();
        // 获取目标消息
        Message message = messageService.getById(msgId);
        // 发送消息给目标房间
        // todo
//        webSocketService.sendToUid();
    }
}
